package com.bdqn.t382.service.impl;

import com.bdqn.t382.mapper.AccountMapper;
import com.bdqn.t382.pojo.Account;
import com.bdqn.t382.pojo.Page;
import com.bdqn.t382.pojo.TransactionRecord;
import com.bdqn.t382.service.AccountService;
import org.apache.logging.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

import org.springframework.transaction.annotation.Transactional;

@Service("accountService")
public class AccountServiceImpl implements AccountService {
    private Logger logger = org.apache.logging.log4j.LogManager.getLogger(AccountServiceImpl.class);
    @Autowired
    private AccountMapper accountMapper;

    @Override
    public Double getBalance(String cardno) {
        return accountMapper.getBalance(cardno);
    }

    @Override
    public Account login(String cardno, String password) {
        return accountMapper.login(cardno, password);
    }

    @Override
    public Page getPage(String cardno, Date startDate, Date endDate, Integer pageNo, Integer pageSize) {
        Page page = new Page();
        try {
            page.setPageNo(pageNo);//当前页
            page.setPageSize(pageSize);//每页显示的数据量
            Long total = accountMapper.count();//总记录数
            page.setTotalCount(total.intValue());
            int offset = (pageNo - 1) * pageSize;// 计算偏移量
            List<TransactionRecord> list = accountMapper.getPage(cardno,startDate,endDate,offset, pageSize);
            page.setList(list);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return page;
    }



    @Override
    public int updatePassword(String cardno, String oldPassword, String newPassword) {
        return accountMapper.updatePassword(cardno, oldPassword, newPassword);
    }

    @Override
    public boolean isAccountExist(String cardno) {
        return accountMapper.isAccountExist(cardno) > 0;
    }

    @Override
    public int transfer(Account account) {
        return accountMapper.transfer(account);
    }

    @Override
    public Account selectByCardno(String cardno) {
        return accountMapper.selectByCardno(cardno);
    }

    @Override
    public int addAccount(TransactionRecord transactionRecord) {
        return accountMapper.addAccount(transactionRecord);
    }

    @Override
    public Long count() {
        return accountMapper.count();
    }

    @Override
    public Account getAccountByToken(String token) {
        return accountMapper.getAccountByToken(token);
    }
}


